Any sort of application can make use of a workflow library; however, here you will opt for simplicity and build a simple Console Application named WorkflowLibraryClient. Once you make the project, you will need to set a reference not only to your CheckInventoryWorkflowLib.dll and AutoLot.dll assemblies, but also the key WF 4.0 library, System.Activities.dll, which can be found in the .NET tab of the Visual Studio 2010 Add Reference dialog.
Once each assembly reference has been set, update your Program.cs file with the following logic:
using System; using System.Linq; using System.Activities; using System.Collections.Generic; using CheckInventoryWorkflowLib; namespace WorkflowLibraryClient { class Program { static void Main(string[] args) { Console.WriteLine("**** Inventory Look up ****"); // Get user preferences. Console.Write("Enter Color: "); string color = Console.ReadLine(); Console.Write("Enter Make: "); string make = Console.ReadLine(); // Package up data for workflow. Dictionary<string, object> wfArgs = new Dictionary<string, object>() { {"RequestedColor", color}, {"RequestedMake", make} }; try { // Send data to workflow! WorkflowInvoker.Invoke(new CheckInventory(), wfArgs); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
As you have done in other examples, you are using the WorkflowInvoker to spin off the workflow in a synchronous manner. While this is all well and good, how are you to get back the return value of the workflow? Remember, once the workflow terminates, you should get back a formatted response!
The WorkflowInvoker.Invoke() method will return an object implementing the IDictionary<string, object> interface. Because a workflow can return back any number of output arguments, you will need to specify the name of a given output argument as a string value to the type indexer. Update your try/catch logic as so:
try { // Send data to workflow! IDictionary<string, object> outputArgs = WorkflowInvoker.Invoke(new CheckInventory(), wfArgs); // Print out the output message. Console.WriteLine(outputArgs["FormattedResponse"]); } catch (Exception ex) { Console.WriteLine(ex.Message); }
Now, run your program and enter a make and color which is currently in your copy of the Inventory table of the AutoLot database. You’ll see output such as the following:
**** Inventory Look up **** Enter Color: Black Enter Make: BMW Yes sir! We can send you Black BMW as soon as 2/17/2010 9:23:01 PM! Press any key to continue . . .
However, if you enter information for an item not currently in stock, you will not only see output such as:
**** Inventory Look up **** Enter Color: Pea Soup Green Enter Make: Viper Sorry, out of stock Press any key to continue . . .
You will also find a new *.txt file in the \bin\Debug folder of the client application. If you open this in any text editor, you’ll find the “sales memo”:
***** Attention sales team! ***** Please order the following ASAP! 1 Pea Soup Green Viper *********************************
That wraps up your introductory look at the brand new WF 4.0 API. As mentioned at the opening of this chapter, if you have worked with the previous version of the Windows Workflow Foundation API, you can clearly see the entire programming model has been overhauled completely (for the better, in my opinion).
While this chapter has only touched on some of the key aspects of this .NET 4.0 API, I do hope you feel confident that you can dive into the topic further if you are interested.
Source Code The WorkflowLibraryClientproject is included under the Chapter 26 subdirectory.